<template>
	<view class="form-list">
		<scroll-view scroll-y class="list-container">
			<view class="form-group" v-for="(group, groupIndex) in groupedForms" :key="groupIndex">
				<view class="group-title">{{ group.name }}</view>
				<view class="form-item" v-for="(item, index) in group.item" :key="index" @tap="goToForm(item)">
					<view class="form-icon">
						<image class="avatar" :src="item.avatar || '/static/avatar/empty.png'" mode="aspectFill">
						</image>
					</view>
					<view class="form-info">
						<view class="form-title">{{ item.username }}</view>
						<view class="form-desc">{{ item.mobile }}</view>
					</view>
					<uni-icons type="right" size="16" color="#999"></uni-icons>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import router from '../../router'
	export default {
		data() {
			return {
				groupedForms: []
			}
		},
		mounted() {
			// this.init();
		},
		onShow() {
			if (!uni.getStorageSync('isLogin') || !uni.getStorageSync('userInfo')) {
				uni.$router.to('pages/login/login')
			}

			const userinfo = uni.getStorageSync('userInfo')

			if (
				userinfo.d_ids?.text === "其他单位" ||
				userinfo.d_ids?.text === "其他" ||
				userinfo.d_ids?.text === undefined
			) {
				uni.switchTab({
					url: '/pages/index/index'
				})
				setTimeout(() => {
					uni.showToast({
						title: '你无权查看通讯录',
						icon: 'error'
					});
				}, 1000);
			}
			this.init();
		},
		methods: {
			async init() {
				const obj = uniCloud.importObject('contact')
				const res = await obj.getList({}).then(r => {
					this.groupedForms = r.data
				})
			},
			goToForm(item) {
				uni.$router.to({
					url: 'pages/contact/detail',
					params: {
						item: item
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.form-list {
		height: 100vh;
		background-color: #f5f5f5;

		.list-container {
			height: 100%;
			box-sizing: border-box;
			padding: 20rpx;

			.form-group {
				margin-bottom: 30rpx;

				.group-title {
					font-size: 30rpx;
					font-weight: bold;
					color: #666;
					padding: 20rpx 10rpx;
				}

				.form-item {
					display: flex;
					align-items: center;
					padding: 30rpx;
					background-color: #ffffff;
					border-radius: 12rpx;
					margin-bottom: 20rpx;

					.form-icon {
						width: 96rpx;
						height: 96rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						margin-right: 24rpx;
						background-color: #edf2ff;
						border-radius: 16rpx;
					}

					.form-info {
						flex: 1;
						display: flex;
						flex-direction: column;

						.form-title {
							font-size: 32rpx;
							color: #333;
							font-weight: 500;
							margin-bottom: 12rpx;
						}

						.form-desc {
							font-size: 26rpx;
							color: #999;
							line-height: 1.4;
						}
					}
				}
			}
		}
	}

	.form-icon {
		width: 96rpx;
		height: 96rpx;
		margin-right: 24rpx;
		background-color: #edf2ff;
		border-radius: 16rpx;
		overflow: hidden;

		.avatar {
			width: 100%;
			height: 100%;
		}
	}
</style>